<template>
  <div>
    <FormPanel
    	:list="formList"
      formType="edit"
      :isSubmitBtn="false"
    	@formChange="changeHandle"
    />

    <!-- destroyOnClose -->
    <BaseDialog
      :visible.sync="visible"
      title="VIN选择"
      :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}"
    >
      <clueSelection v-if="visible" :productId="productId" :rowData="formData" @close="closeHandler" />
    </BaseDialog>

    <div
      :style="{
        position: 'absolute',
        left: 0,
        bottom: 0,
        right: 0,
        zIndex: 9,
        borderTop: '1px solid #e9e9e9',
        padding: '10px 20px',
        background: '#fff',
        textAlign: 'right'
      }"
    >
      <AjaxButton :disabled="saveData==null" :click="saveDialog" type="primary">保 存</AjaxButton>
      <el-button @click="closeDialog">返 回</el-button>
    </div>
  </div>
</template>

<script>
import { dictionary } from '@/utils/dictMixin'; //数据字典
import { parseTime } from '@/utils/index'; //时间过滤
import clueSelection from './clueSelection';
import moment from 'moment'
import { getAllocationDetails, vehiclePreservation } from '@/api/retail/salesmanage/confirmcar';
export default {
  name: 'BuyingModal',
  mixins: [dictionary],
  components: {
    clueSelection
	},
  props: {
    type: {
      type: String,
      default: 'show'
    },
    rowData: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
    	formList: this.createFormList(), //上方搜索数据
      visible: false,
      formData: {},
      saveData: null,
      productId:null,

    };
  },
  created (){
    this.init();
  },
  methods: {
    init (){
      console.log(this.rowData)
      this.getAllDetail(this.rowData.soVinId);
    },
    async getAllDetail (row){
      let res = await getAllocationDetails(row);
      if(res.resultCode == 200){
        console.log(res,"数据")
        this.productId = `${res.data.brandId},${res.data.seriesId},${res.data.modelId},${res.data.packageId},${res.data.colorId}`
        res.data.deliveringDate = this.preDateHandler(res.data.deliveringDate, 'YYYY-MM-DD')
        this.formList.forEach(x => x.initialValue = res.data[x.fieldName] );
      }
    },
    createFormList (){
    	return [
    		{
          type: 'INPUT',
          label: '客户订单号',
          fieldName: 'soNo',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '销售订单号',
          fieldName: 'viNo',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '客户编号',
          fieldName: 'customerNo',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'SELECT',
          label: '订单状态',
          fieldName: 'soStatus',
          placeholder: '',
          disabled: true,
          itemList: this.createDictList('1404'),
        },
        {
          type: 'INPUT',
          label: '客户名称',
          fieldName: 'customerName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '手机号码',
          fieldName: 'mobilePhone',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '产品代码',
          fieldName: 'productCode',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '产品名称',
          fieldName: 'productName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: 'VIN',
          fieldName: 'salesVin',
          placeholder: '请选择...',
          readonly: true,
          unitRender: () => {
	        	return <el-button slot="append" icon="el-icon-search" onClick={this.VINChoice}></el-button>;
	        },
        },
        {
          type: 'INPUT',
          label: '发动机号',
          fieldName: 'engineNo',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '合格证书',
          fieldName: 'certificateNumber',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '预交车日期',
          fieldName: 'deliveringDate',
          placeholder: '',
          disabled: true,
          // render: props =>{
          //   return <span>{ this.preDateHandler(props.row.deliveringDate,'YYYY-MM-DD') } </span>
          // }
        },
        {
          type: 'INPUT',
          label: '品牌',
          fieldName: 'brandName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车系',
          fieldName: 'seriesName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '配置',
          fieldName: 'packageName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '颜色',
          fieldName: 'colorName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '销售指导价',
          fieldName: 'directivePrice',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '销售单价',
          fieldName: 'vehiclePrice',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '备注',
          fieldName: 'remark',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '开票名称',
          fieldName: 'customerName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '开票联系人',
          fieldName: 'contactName',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '开票电话',
          fieldName: 'phone',
          placeholder: '',
          disabled: true,
        },
        {
          type: 'INPUT',
          label: '开票地址',
          fieldName: 'address',
          placeholder: '',
          disabled: true,
        },




    	]
    },
    changeHandle(val){
      console.log('搜索的参数：', val)
    },
    VINChoice (val){ //vin选择
      this.visible = true;
    },
    saveDialog (){ //保存
      let salesVin = this.formList.find(item => item.fieldName == 'salesVin').initialValue
      let engineNo = this.formList.find(item => item.fieldName == 'engineNo').initialValue
      let certificateNumber = this.formList.find(item => item.fieldName == 'certificateNumber').initialValue
      if(salesVin){
        let para = {
          soVinId: this.rowData.soVinId,
          vin: this.saveData.vin,
          vsStockId: this.saveData.vsStockId,
        }
        console.log(para)
        vehiclePreservation(para).then(res => {
          console.log(res)
          this.closeDialog('refresh'); //成功关闭抽屉
        }).catch(err => {
          console.log(err)
        })
      }else {
        this.$message({
          message: '没有选择VIN，不可保存',
          type: 'warning'
        });
      }
    },
    closeDialog (para){ //关闭抽屉
      this.$emit('close', false, para)
    },
    closeHandler (val, row){ //关闭弹窗
      // console.log(row[0])
      if(row){
        this.saveData = row[0];
        this.formList.find(item => item.fieldName == 'salesVin').initialValue = this.saveData.vin
        this.formList.find(item => item.fieldName == 'engineNo').initialValue = this.saveData.engineNo
        this.formList.find(item => item.fieldName == 'certificateNumber').initialValue = this.saveData.certificateNumber
        this.visible = val;
      }else {
        this.visible = val;
      }
    },
    // 日期预处理
    preDateHandler(val, formatCode='YYYY-MM-DD HH:mm:ss') {
      let resultDate = val ? moment(Number(val)).format(formatCode) : val;
      return resultDate;
    },
    formatDates (value) { //时间过滤
        // 这里的 value 就是需要过滤的数据
        if (value == null || value == "") {
          return "";
        }
        var date = new Date(Number(value));
        var year = date.getFullYear();
        var month = date.getMonth() + 1;
        var day = date.getDate();
        return year + "-" + month + "-" + day;
      },

  }
};
</script>

<style lang="less" scoped>
</style>
